package cn.iocoder.yudao.module.ride.controller.admin.blacklist;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.blacklist.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.blacklist.BlacklistDO;
import cn.iocoder.yudao.module.ride.service.blacklist.BlacklistService;

@Tag(name = "管理后台 - 黑名单记录")
@RestController
@RequestMapping("/ride/blacklist")
@Validated
public class BlacklistController {

    @Resource
    private BlacklistService blacklistService;

    @PostMapping("/create")
    @Operation(summary = "创建黑名单记录")
    @PreAuthorize("@ss.hasPermission('ride:blacklist:create')")
    public CommonResult<Long> createBlacklist(@Valid @RequestBody BlacklistSaveReqVO createReqVO) {
        return success(blacklistService.createBlacklist(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新黑名单记录")
    @PreAuthorize("@ss.hasPermission('ride:blacklist:update')")
    public CommonResult<Boolean> updateBlacklist(@Valid @RequestBody BlacklistSaveReqVO updateReqVO) {
        blacklistService.updateBlacklist(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除黑名单记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:blacklist:delete')")
    public CommonResult<Boolean> deleteBlacklist(@RequestParam("id") Long id) {
        blacklistService.deleteBlacklist(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除黑名单记录")
                @PreAuthorize("@ss.hasPermission('ride:blacklist:delete')")
    public CommonResult<Boolean> deleteBlacklistList(@RequestParam("ids") List<Long> ids) {
        blacklistService.deleteBlacklistListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得黑名单记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:blacklist:query')")
    public CommonResult<BlacklistRespVO> getBlacklist(@RequestParam("id") Long id) {
        BlacklistDO blacklist = blacklistService.getBlacklist(id);
        return success(BeanUtils.toBean(blacklist, BlacklistRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得黑名单记录分页")
    @PreAuthorize("@ss.hasPermission('ride:blacklist:query')")
    public CommonResult<PageResult<BlacklistRespVO>> getBlacklistPage(@Valid BlacklistPageReqVO pageReqVO) {
        PageResult<BlacklistDO> pageResult = blacklistService.getBlacklistPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BlacklistRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出黑名单记录 Excel")
    @PreAuthorize("@ss.hasPermission('ride:blacklist:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlacklistExcel(@Valid BlacklistPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlacklistDO> list = blacklistService.getBlacklistPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "黑名单记录.xls", "数据", BlacklistRespVO.class,
                        BeanUtils.toBean(list, BlacklistRespVO.class));
    }

}